package com.kaifamiao.www.demo.do0826;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class Demo {
    /**
     * 计算下一个生日还有多久
     * @param date 生日字符串  格式  MM - dd 例如  10-01
     * @return
     */
    public static long getNextBirthdayDays(String date){
        //判断字符串是否为空
        if (date == null){
            throw new NullPointerException("传入的字符串不规范");
        }
        //现在的时间
        LocalDate now = LocalDate.now();
        //今年生日的日期字符串
        String birthday = now.getYear()+ "-" + date;
        //转换为localDate
        LocalDate next = LocalDate.parse(birthday);
        //方案一 toEpochDay（）日期转换为纪元日 ，从1970-01-01-到指定日期的天数
        long nowDays = now.toEpochDay();
        long birthdayDays = next.toEpochDay();
        if (now.isAfter(next)) {
            //现在的时间在生日之后，说明生日已经过去，后一年
//            next = next.plus(1, ChronoUnit.YEARS);
            next = next.plusYears(1);
            birthdayDays = next.toEpochDay();
        }
        return birthdayDays - nowDays;
    }
    public static long getNextBirthdayDays2(String date){
        //判断字符串是否为空
        if (date == null){
            throw new NullPointerException("传入的字符串不规范");
        }
        //现在的时间
        LocalDate now = LocalDate.now();
        //今年生日的日期字符串
        String birthday = now.getYear()+"-"+date;
        //转换为localDate
        LocalDate next = LocalDate.parse(birthday);
        //方案二  until计算两个日期间的差值
        if (now.isAfter(next)){
            //今年生日过了
            next = next.plusYears(1);

        }
        return now.until(next,ChronoUnit.DAYS);
    }
    public static long getAnniversaryDaysAfter(String date){
        if (date == null){
            throw new NullPointerException("出错啦");
        }
        LocalDate now = LocalDate.now();
        LocalDate anniversary = LocalDate.parse(date);
        return anniversary.until(now,ChronoUnit.DAYS);
    }
    public static long get(String date,String otherdate){
        //日期格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //将给定字符串解析成日期
            Date d = dateFormat.parse(date);
            Date d1 = dateFormat.parse(otherdate);
            //返回自 1970 年 1 月 1 日 00：00：00 GMT 以来此对象表示的毫秒数。
            long time = d.getTime() - d1.getTime();
            double floor = Math.floor(time / 1000.0/60 / 60 /24);
            return Math.round(floor);

        } catch (ParseException e){
            e.printStackTrace();
        }
        return -1;
    }

    public static void main(String[] args) {
        String birthday = "06-27";
        System.out.println(getNextBirthdayDays(birthday));
        System.out.println(getNextBirthdayDays2(birthday));

        String anniversary = "2022-07-11";
        System.out.println(getAnniversaryDaysAfter(anniversary));

        String d = "2022-07-14";
        String d1 = "2022-08-26";
        System.out.println(get(d,d1));
    }
}
